package demo.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import demo.entity.emp_test;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import demo.entity.users;
import demo.returnMsg.Msg;
import demo.service.LoginService;

import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
public class LoginController {
    private Logger logger = Logger.getLogger(LoginController.class);

	@Autowired
    private LoginService loginService;
	
	@PostMapping(value = "/login")
	public Map login(HttpServletRequest request, @RequestParam(value = "emp_id") String emp_id, @RequestParam String password){
        HashMap<String,Object> response = new HashMap<>();
        HashMap<String,Object> responseData = new HashMap<>();
	    System.out.println(emp_id);
	    users user = new users();
		user.setEmp_id(Integer.parseInt(emp_id));
		user.setPassword(password);
        Msg msg = loginService.login(user);
        HttpSession session = request.getSession();
        session.setAttribute("Permission", msg.getPermission());
        responseData.put("token",3);
        response.put("code",200);
        response.put("msg","login-success");
        response.put("data",responseData);
		return response;
    }

	@GetMapping("/logout")
    public void logout(HttpSession session) {
        // 移除session
        session.removeAttribute("Permission");
    }
}
